package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.CityAdapter;
import com.luck.easyfoodbatch.db.PlaceDB;
import com.luck.easyfoodbatch.entity.CityEntity;
import com.luck.easyfoodbatch.entity.Place;
import com.luck.easyfoodbatch.indexablelistview.IndexEntity;
import com.luck.easyfoodbatch.indexablelistview.IndexableStickyListView;
import com.luck.easyfoodbatch.mvp.home.CountryModel;
import com.luck.easyfoodbatch.mvp.home.CountryPresenter;
import com.luck.easyfoodbatch.mvp.home.CountryView;
import com.luck.easyfoodbatch.mvp.model.Country;
import com.luck.easyfoodbatch.util.HttpUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectionPointActivity extends BaseActivity implements CountryView {

    @BindView(R.id.indexListView)
    IndexableStickyListView indexListView;
    private final String EXTRA_LARGE = "extra_large";
    @BindView(R.id.input_search_query)
    EditText inputSearchQuery;
    @BindView(R.id.ll_del)
    LinearLayout llDel;
    @BindView(R.id.rl_city)
    RelativeLayout rl_city;
    private CityAdapter mAdapter;
    private List<CityEntity> mCities = new ArrayList<>();
    private List<Place> list = new ArrayList<>();
    private PlaceDB placeDB;
    private CountryModel countryModel;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 200:
                    initView();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selection_point, "选择产地", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        placeDB = new PlaceDB(this);
        countryModel = new CountryPresenter(this, mContext);
        new Thread(new Runnable() {
            @Override
            public void run() {
                list = placeDB.getByAllPlaceList();
                handler.sendEmptyMessage(200);
            }
        }).start();

        //showProgressDialog(getString(R.string.app_loading));
        //countryModel.getCountryList(0);
        inputSearchQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 0) {
                    llDel.setVisibility(View.VISIBLE);
                } else {
                    llDel.setVisibility(View.GONE);
                }
            }
        });
    }

    protected void initView() {
        mAdapter = new CityAdapter(this);
        indexListView.setAdapter(mAdapter);

        // 初始化数据
        mCities.clear();
        for (Place item : list) {
            CityEntity cityEntity = new CityEntity();
            cityEntity.setName(item.getName());
            cityEntity.setId(item.getPid());
            mCities.add(cityEntity);
        }

        // 绑定数据
        indexListView.bindDatas(mCities);
        // 搜索
        inputSearchQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                indexListView.searchTextChange(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        indexListView.setOnItemContentClickListener(new IndexableStickyListView.OnItemContentClickListener() {
            @Override
            public void onItemClick(View v, IndexEntity indexEntity) {
                Intent intent = new Intent();
                intent.putExtra("name", indexEntity.getName());
                intent.putExtra("country_id", indexEntity.getId());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @OnClick({R.id.ll_del, R.id.rl_city})
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.ll_del:
                inputSearchQuery.setText("");
                break;
            case R.id.rl_city:
                intent.putExtra("name", "中国");
                intent.putExtra("country_id", "1");
                setResult(RESULT_OK, intent);
                finish();
                break;
        }

    }

    @Override
    public void getCountrySuccess(Country model) {
        try {
            if (model != null) {
                if (model.getStatus() == HttpUtil.RESULT_OK) {
                    List<Country.DataBean> data = model.getData();
                    // 保存国家到本地
                    if (data != null && data.size() > 0) {
                        placeDB.deleteTable();
                        placeDB.savePlaceList(data);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }
}
